package socket;

import org.junit.Test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

/**
 * @author: feiwang_6
 * @create: 2020/6/8 14:52
 * @description:
 */
public class SocketTest {
    
    
    public void test(){
            
    }

    /**
     * 获取远程文件和大小
     * @throws IOException
     */
    @Test
    public void testRemoteFile() throws IOException {
        URL url = new URL("https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.5.0.tar.gz");
        URLConnection urlConnection = url.openConnection();
        //文件大小
        System.out.println(urlConnection.getContentLengthLong());
        InputStream is = url.openStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        StringBuffer sb = new StringBuffer();
        int len = 0;
        byte[] temp = new byte[1024];
        while ((len = bis.read(temp)) != -1) {
            sb.append(new String(temp, 0, len));
        }
        sb.toString();
    }
    
    @Test
    public void testSocket() throws Exception {
        Thread thread1 = new Thread(() -> {
            try {
                socketService();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

        Thread threadA = new Thread(() -> {
            try {
                socketClientA();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        Thread threadB = new Thread(() -> {
            try {
                socketClientB();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        thread1.start();
        Thread.sleep(500);
        threadA.start();
        Thread.sleep(500);
        threadB.start();
        threadA.join();
        threadB.join();
    }
    
    public void socketService() throws Exception {
        //创建一个服务器端的Socket
        ServerSocket serverSocket = new ServerSocket(8000, 2);
        System.out.println("服务器已经启动，等待客户端连接");
        //侦听，如果连接成功，则返回一个Socket对象
        BufferedReader br = null;
        int i = 2;
        while (i > 0){
            System.out.println("开始客户端连接...");
            Socket socket = serverSocket.accept();
            System.out.println("客户端连接成功!");
            
            //读取客户端的数据
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("服务端读数据...");
            String str = br.readLine();
            System.out.println("接受到客户端的消息：" + str);
            i --;
        }
        //br.close();

        //服务器向客户端写入数据
        /*Thread.sleep(8000);
        PrintStream ps=new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
        ps.println("服务端发给客户端的数据....");
        ps.flush();
        ps.close();*/
        
    }
    
    public void socketClientA() throws Exception {
        Socket socket = new Socket("localhost",8000);
        PrintStream ps= new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
        Thread.sleep(10000);
        System.out.println("客户端A开始写数据.....");
        ps.println("hello word A!!");
        ps.flush();
        ps.close();

        /*BufferedReader br =new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("客户端开始读取数据!");
        String info = br.readLine();
        System.out.println(info);
        br.close();*/
    }

    public void socketClientB() throws Exception {
        Socket socket = new Socket("localhost",8000);
        PrintStream ps= new PrintStream(new BufferedOutputStream(socket.getOutputStream()));
        System.out.println("客户端B开始写数据.....");
        ps.println("hello word B!!");
        System.out.println("11111111111111111");
        ps.flush();
        ps.close();
        
        /*BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        System.out.println("客户端开始读取数据!");
        String info = br.readLine();
        System.out.println(info);
        br.close();*/
    }
}
